import axios from "axios";

const service = axios.create({

});
/**
 * 请求拦截
 */
service.interceptors.request.use(
    (config) => {
        let token = "";
        if (sessionStorage.getItem("userInfo")) {
            token = JSON.parse(sessionStorage.getItem("userInfo")).token;
            if (config.url.indexOf("upload.yl1001.com") < 0) {
                config.headers["authorization"] = token;
                config.headers["Content-Type"] = "application/json; charset=UTF-8";
            } else {
                config.headers["Content-Type"] = "application/x-www-form-urlencoded";
            }
            return config;
        }
    },
    (err) => {
        // tryHideFullScreenLoading();
        // Message.error({ message: '请求超时!' });
        return Promise.resolve(err);
    }
);
/**
 * 响应拦截
 */
service.interceptors.response.use(
    (res) => {
        // tryHideFullScreenLoading();
        if (parseInt(res.data.code) === 200) {
            return Promise.resolve(res.data);
        } else {
            return Promise.resolve(res.data);
        }
    },
    (err) => {
        // tryHideFullScreenLoading();
        return Promise.reject(err);
    }
);
/**
 * 请求地址处理
 * @param {*} actionName action方法名称
 */
service.adornUrl = (actionName, who) => {
    // 非生产环境 && 开启代理, 接口前缀统一使用[/proxyApi/]前缀做代理拦截!
    var actionArr = actionName.split("/");
    var tmpAr = [];
    for (var i in actionArr) {
        if ((actionArr.length > 3 && i > 1) || (actionArr.length > 2 && i > 0)) {
            tmpAr.push(actionArr[i]);
        }
    }
    //添加多ip提供接口代理
    var proxyApi = "";
    if (who) {
        switch (who) {
            case "proxyLS":
                proxyApi = "/proxyLS";
                break;
            default:
                break;
        }
    }
    // 非生产环境 && 开启代理, 接口前缀统一使用[/proxyApi/]前缀做代理拦截!
    return (
        (process.env.NODE_ENV !== "production" ? proxyApi : "http://producttest.yl1001.com/") + tmpAr.join("/")
    );
};

export default service;
